<template>
    <transition>
        <el-dialog :title="dialogType === 'add' ? '添加课程信息' : '修改课程信息'" v-model="showFlag" width="600px" append-to-body
            @close="handleClose" @open="handleOpen">
            <el-form hide-required-asterisk ref="formRef" :rules="formRule" :model="formInfo" label-width="auto"
                style="padding:30px 30px 0 30px">
                <el-form-item label="年级" prop="grade">
                    <el-input placeholder="年级" type="number" v-model="formInfo.grade"></el-input>
                </el-form-item>
                <el-form-item label="班级名称" prop="name">
                    <el-input placeholder="班级名称" type="text" v-model="formInfo.name"></el-input>
                </el-form-item>
                <el-form-item label="专业" prop="majorId">
                    <el-select placeholder="专业" v-model="formInfo.majorId">
                        <el-option v-for="e in majors" :key="e" :value="e.id" :label="e.name"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="学生人数" prop="studentCount">
                    <el-input placeholder="学生人数" type="number" v-model="formInfo.studentCount"></el-input>
                </el-form-item>
                <el-form-item label="院系" prop="departmentId">
                    <el-select placeholder="院系" v-model="formInfo.departmentId">
                        <el-option v-for="e in departments" :key="e" :value="e.id" :label="e.name"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item style="margin-top: 40px; margin-left: 372px; ">
                    <el-button @click="handleClose" size="mini">取消</el-button>
                    <el-button @click="formSubmit" size="mini" type="primary">确定</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>
    </transition>
</template>

<script setup>
import api from '@/api/api';
import { ElMessage } from 'element-plus';
import { onMounted, reactive, ref, watch } from 'vue';

const props = defineProps({
    dialogType: {
        type: String,
        default: 'add'
    }, showDialog: {
        type: Boolean
    },
    applyId: {
        default: ''
    },
    editForm: {
        type: Object
    }
})
const emit = defineEmits(['dialogClosed'])
const showFlag = ref(false)

const majors = ref([
    // id, name
])
const departments = ref([
    // id, name
])

const formRef = ref(null)

const validateGrade = (rule, value, callback) => {
    let num = parseInt(value)
    if (2000 > num || num > 2050) {
        callback(new Error('请输入2000-2050数字'))
    }
    callback()
}
const validateStudentCount = (rule, value, callback) => {
    let num = parseInt(value)
    if (0 > num || num > 3000) {
        callback(new Error('请输入0-3000数字'))
    }
    callback()
}
const formRule = ref({
    grade: [
        { required: true, message: '请填写年级', trigger: 'blur' },
        { validator: validateGrade, trigger: 'blur' },
    ],
    name: [
        { required: true, message: '请填写班级名称', trigger: 'blur' }
    ],
    majorId: [
        { required: true, message: '请填写专业', trigger: 'blur' }
    ],
    studentCount: [
        { required: true, message: '请填写学生人数', trigger: 'blur' },
        { validator: validateStudentCount, trigger: 'blur' },
    ],
    departmentId: [
        { required: true, message: '请填写院系', trigger: 'blur' }
    ],
})
const formInfo = reactive(
    {
        'grade': '', 'name': '', 'majorId': '', 'studentCount': '',
        'departmentId': '',
    },
)
const formSubmit = () => {
    formRef.value.validate((valid) => {
        if (valid) {
            if (props.dialogType == 'add') {
                api.class_js.add(
                    formInfo.grade, formInfo.name, formInfo.majorId,
                    formInfo.studentCount, formInfo.departmentId
                ).then((res) => {
                    ElMessage({
                        message: '添加成功',
                        type: 'success'
                    })
                }).catch((err) => {
                    console.log(err)
                })

                handleClose()
            } else {
                api.class_js.edit(
                    props.applyId,
                    formInfo.grade, formInfo.name, formInfo.majorId,
                    formInfo.studentCount, formInfo.departmentId
                ).then((res) => {
                    ElMessage({
                        message: '修改成功',
                        type: 'success'
                    })
                }).catch((err) => {
                    console.log(err)
                })

                handleClose()
            }

        }
    })
}
const handleClose = () => {
    showFlag.value = false
    formRef.value.resetFields()
    emit('dialogClosed')
}
const handleOpen = () => {
    if (props.dialogType === 'edit') {
        formInfo.grade = props.editForm.grade
        formInfo.name = props.editForm.name
        formInfo.majorId = props.editForm.majorId
        formInfo.studentCount = props.editForm.studentCount
        formInfo.departmentId = props.editForm.departmentId
    }
}

watch(() => props.showDialog, (newValue, oldValue) => {
    showFlag.value = props.showDialog
})
onMounted(() => {
    api.major.get_all().then((res) => {
        majors.value = res.data.data
    }).catch((err) => {
        console.log(err)
    })
    api.department.get_all().then((res) => {
        departments.value = res.data.data
    }).catch((err) => {
        console.log(err)
    })
})
</script>